package com.edu.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Service;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.edu.gateway.base.GatewayHelper;
import com.edu.gateway.utils.RedisUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class AuthGatewayFilterFactory  extends AbstractGatewayFilterFactory {
    @Override
    public GatewayFilter apply(Object config) {
        return (exchange, chain) -> {
            String token=exchange.getRequest().getHeaders().getFirst("LOGINTOKEN");
            log.info("login filter token:{}",token);
            String juser=RedisUtils.get(token);
            log.info("login filter user:{}",juser);
            if(StringUtils.isBlank(juser)){
                return GatewayHelper.noAuth(exchange);
            }
            return chain.filter(exchange);
        };
    }
}
